<?php
/**
 * Created by PhpStorm.
 * User: 勇林
 * Date: 2015/12/11
 * Time: 11:10
 */

namespace app\api\controllers;

use app\api\Response;
use yii\helpers\ArrayHelper;
use app\models\Prize;
use yii\base\Model;

use yii\web\Controller;

class PrizeController extends Controller
{
    public function actionList($pageNow, $pageSize){

        $list = Prize::getList($pageNow, $pageSize);
        Response::setHeader();
        echo json_encode($list);
    }

    public function actionDetail($id){

        $detail = Prize::findOne(['id'=>$id]);
        Response::setHeader();
        echo json_encode(ArrayHelper::toArray($detail));
    }
    
      public function actionSearch($prizename){
        $prizes= Prize::find()
              ->where(['or',['like','members',$prizename],['like','prizename',$prizename]])
              ->all();
      $results = [];
        foreach($prizes as $model){
            $dname='';
            if($model->dept){
                $dname = $model->dept->dname;
            }  else {
                $dname = '未设置';
            }
            $result = [
                'id'=>$model->id,
                'prizename'=>$model->prizename,
                'dept'=>$dname,
                'addtime'=>$model->addtime
            ];
            $results[] = $result;
        }
      Response::setHeader();
      return json_encode(\yii\helpers\ArrayHelper::toArray($results));
    }
}